﻿using CPPEI.Coolzon.Event.NCL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI.Views
{
    public partial class LoginView
    {
        [Common.Subscribe]
        public void OnReceiveNCLLoginEventEvent(NCLLoginEvent @event)
        {
            this.Dispatcher.BeginInvoke(new Action<NCLLoginEvent>(OnInvokeReceiveNCLLoginEventEvent), @event);
        }

        private void OnInvokeReceiveNCLLoginEventEvent(NCLLoginEvent @event)
        {
            if (@event.State == NCLEventState.Failure)
            {
                System.Windows.MessageBox.Show("登录失败，出现错误。",
                    "错误",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);

                _ctlBorderLogin.Visibility = System.Windows.Visibility.Visible;
                _ctlBorderIndicator.Visibility = System.Windows.Visibility.Hidden;
            }
            else if (@event.State == NCLEventState.Finished)
            {
                // 登录成功
                UIContext.Current.UserAccount = UserAccount;
                UIContext.Current.UserName = @event.Result.UserName;
                UIContext.Current.UserIdentity = @event.Result.UserIdentity;
                _ctlBorderLogin.Visibility = System.Windows.Visibility.Visible;
                _ctlBorderIndicator.Visibility = System.Windows.Visibility.Hidden;
                this.Submit();
            }
        }
    }
}
